<?php
class productControl extends skymvc{
	public $userid;
	public function __construct(){
		parent::__construct();
	}
	
	public function onInit(){
		if(in_array(get('a'),array("my","save","add"))){
			M("login")->checklogin();
		}
		$this->userid=M("login")->userid;
	}
	
	function onDefault(){
		 
		$rscount=true;
		 
		$where=" status=2 ";
		$url="/index.php?m=product";
		$keyword=get_post('keyword','h');
		if($keyword){
			$where.=" AND title like '%".$keyword."%'";
			$url.="&keyword=".urlencode($keyword);
		}
		$attr_1=get_post('attr_1','i');
		$attr_2=get_post('attr_2','i');
 
		$attr_3=get_post('attr_3','i');
		$attr_4=get_post('attr_4','i');
		if($attr_1){
			$where.=" AND attr_1=".$attr_1;
			$url.="&attr_1=".$attr_1;
		}
		if($attr_2){
			$where.=" AND attr_2=".$attr_2;
			$url.="&attr_2=".$attr_2;
		}
		if($attr_3){
			$where.=" AND attr_3=".$attr_3;
			$url.="&attr_3=".$attr_3;
		}
		if($attr_4){
			$where.=" AND attr_4=".$attr_4;
			$url.="&attr_4=".$attr_4;
		}
		$brand_id=get('brand_id','i');
		if($brand_id){
			$where.=" AND brand_id=".$brand_id;
			$url.="&brand_id=".$brand_id;
		}
 
		switch(get('orderby')){
			case "dateline":
					$order="id DESC";
				break;
			case "price":
					$order="price ASC";
				break;
			default :
					$order="buy_num DESC,id DESC";
				break;
		}
		$start=get_post('per_page','i');
		$limit=20;
		$option=array(
			"where"=>$where,
			"start"=>$start,
			"limit"=>$limit,
			"order"=>$order
		);
		$data=M("product")->select($option,$rscount);
		if($data){
			$t_ids=array();
			foreach($data as $k=>$v){
				$t_ids[]=$v['catid'];
				$t_uids[]=$v['userid'];
			}
			if($t_ids){
				$t_c=M("category")->cat_list(" catid in("._implode($t_ids).")");
			}
			
			if($t_uids){
				$t_u=M("user")->getUserByIds($t_uids);
			}
			foreach($data as $k=>$v){
				$v['cname']=$t_c[$v['catid']];
				if(isset($t_u[$v['userid']])){
					$v['nickname']=$t_u[$v['userid']]['nickname'];
				}
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->assign(array(
			"list"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"url"=>$url
		));
		$this->smarty->display("product/index.html");
		
	}

	 
	
	public function onAddClick(){
		$id=get_post('id','i');
		$row=M("product")->selectRow("id=".$id);
		if($row){
			M("product")->update(array("view_num"=>$row['view_num']+1),"id=".$id); 
		}
		 
	 }
	
}
?>